package com.xlh.dto.courseTest;

import com.google.common.collect.Lists;
import com.xlh.dto.course.ClassCourseSearchDTO;
import com.xlh.dto.course.StatisticsDTO;
import com.xlh.dto.courseTest.practice.PracticeClassDTO;
import com.xlh.util.BeanUtil;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhr 2019/4/15
 */
@Data
public class ClassCheckCountDTO {
    // 班级id
    private Long id;
    // 班级名称
    private String name;
    // 批阅总数
    private Long count;
    // 课程名称
    private String courseName;
    // 学生id
    private Long studentId;
    //年纪
    private String grade;
    private String is_ended;
    private Long courseId;
    public static StatisticsDTO convert2StatisticsDTO(List<ClassCheckCountDTO> list) {
        StatisticsDTO dto = new StatisticsDTO();
        if (CollectionUtils.isNotEmpty(list)) {
            List<ClassCourseSearchDTO> classes = BeanUtil.batchTransform(ClassCourseSearchDTO.class, list);
            Long count = list.stream().map(ClassCheckCountDTO::getCount).reduce(0L, Long::sum);
            dto.setClasses(classes);
            dto.setCount(count);
        }
        return dto;
    }
    public static StatisticsDTO convert2StatisticsDTONew(List<ClassCheckCountDTO> list) {
        StatisticsDTO dto = new StatisticsDTO();
        List<PracticeClassDTO> pralist= Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(list)) {
            List<ClassCourseSearchDTO> classes = BeanUtil.batchTransform(ClassCourseSearchDTO.class, list);
            Long count = list.stream().map(ClassCheckCountDTO::getCount).reduce(0L, Long::sum);
            dto.setClasses(classes);
            dto.setCount(count);
            for (ClassCheckCountDTO classCheckCountDTO:list ) {
                PracticeClassDTO practiceClassDTO=new PracticeClassDTO();
                practiceClassDTO.setClassName(classCheckCountDTO.getName());
                practiceClassDTO.setName(classCheckCountDTO.getCourseName());
                practiceClassDTO.setCourseId(classCheckCountDTO.getCourseId());
                pralist.add(practiceClassDTO);
            }
            List<PracticeClassDTO> copmpaeList=pralist.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()
                    -> new TreeSet<>(Comparator.comparing(dto1 ->dto1.getClassId()+"|"+dto1.getCourseId()))), ArrayList::new));
            List<PracticeClassDTO> groupList = new ArrayList<>(new HashSet<>(copmpaeList));
            dto.setCourseIAndClass(groupList);
        }
        return dto;
    }
}
